<?php
/**
 * Framework Config
 * 
 * @category	Configuration
 * @package		dayscript.config
 * @subpackage	
 * @author		Nelson Daza <ndaza@dayscript.com>
 * @copyright	2009 Dayscript Ltda.
 * @license		
 * @version		1.0
 * @version		$Revision: 0 $
 * @filesource	
 * @link		http://www.dayscript.com
 * @link		{docsLink}
 * @uses		
 * @since		1.0
 * @modifiedby	$LastChangedBy: Nelson Daza $
 * @modified	$Date: 2009-08-21 $
 */

namespace dayscript\config;

/**
 * General Config implementation
 * 
 * @category	Configuration
 * @package		dayscript.config
 * @subpackage	
 * @author		Nelson Daza <ndaza@dayscript.com>
 * @copyright	2009 Dayscript Ltda.
 * @license		
 * @version		1.0
 * @link		http://www.dayscript.com
 * @link		{docsLink}
 * @uses		\dayscript\Map, \dayscript\config\ApplicationDefaults
 * @since		1.0
 * @modifiedby	$LastChangedBy: Nelson Daza $
 * @modified	$Date: 2009-08-25 $
 */
class Config	{
	/**
	 * @var Map $defaults Holds all loaded values
	 */
	private $values = null;
	/**
	 * Constructs a new Config.
	 *
	 * @return Config
	 */
	public function __construct( )	{
		$this->values = new \dayscript\Map( );
	}
	/**
	 * Returns a map key/value
	 * 
	 * @return \dayscript\Map
	 */
	public function getValues( )	{
		return $this->values;
	}
	/**
	 * Loads all Key => Values from a file.
	 *
	 * This version requieres at least one php file with values based on the \dayscript\config\ApplicationDefaults in a Map $config variable.
	 * When using an array of paths, the order of the files will be the exact order walking into the array using a foreach function.
	 * Be warned that every value can be overwritten in the next file, so after the first file you must not use $defaults.
	 *
	 * @param string|array $filePath String or Array of Strings ubications for configuration files.
	 * @param \dayscript\Map $defaultValues
	 * @return \dayscript\Map $values
	 * @see \dayscript\config\ApplicationDefaults
	 */
	public function loadConfig( $filePath, \dayscript\Map $defaultValues = null )	{
		$defaults = new \dayscript\util\Properties( $defaultValues, true );
		unset( $defaultValues );
		
		if( !\is_array( $filePath ) )
			$filePath = array( $filePath );
		$configFiles = new \dayscript\util\Properties( $filePath, true );
		unset( $filePath );
		
		$config = new \dayscript\Map( );
		
		foreach( $configFiles as $actualFile )	{
			\dayscript\log\LogManager::log( 'CONFIG Loading file: "' . $actualFile . '"', \dayscript\log\LogManager::LOG_USER_INFO );
			require( $actualFile );
		}
		
		$this->values->putAll( $defaults );
		$this->values->putAll( $config );
		
		return $this->values;
	}
}